<?php
//封装网络请求, 当php服务器需要发起请求时, 导入该文件

//curl模块来发起请求(微信官方推荐方式)

//HTTP/HTTPS
//1.get
function httpGet($url) {
	//初始化curl
	$curl = curl_init();
	//配置curl
	//1.获取的信息以文件流的形式返回
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	//2.超时时间
	curl_setopt($curl, CURLOPT_TIMEOUT, 500);
	//3.请求的url
	curl_setopt($curl, CURLOPT_URL, $url);
	//如果支持https
	//4.开启ssl验证
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
	//5.允许所有主机地址, 2代表所有
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
	//执行url, 并获取数据
	$response = curl_exec($curl);
	//断开链接
	curl_close($curl);
	//返回数据
	return $response;
}

//2.post
function httpPost($url, $data, $isJson=false) {
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_TIMEOUT, 500);
	curl_setopt($curl, CURLOPT_URL, $url);
	//设置post请求的参数
	curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	//注: 默认的post参数的格式: application/x-www-form-urlencoded, 比如a=x&b=x$c=x
	if ($isJson) {
		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
			'Content-type:application/json'
		));
	}
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
	$response = curl_exec($curl);
	curl_close($curl);
	return $response;
}

?>